package com.tengits.mqttmonitor.rnlibs.logs;

import android.os.Environment;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.tencent.mars.xlog.Log;

public class XLogLibraryModule extends ReactContextBaseJavaModule {
    private static final String TAG = "ReactNative";
    public XLogLibraryModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @NonNull
    @Override
    public String getName() {
        return "XLogLibraryModule";
    }

    @ReactMethod
    public void i(String msg) {
        Log.i(TAG, msg);
    }

    @ReactMethod
    public void d(String msg) {
        Log.d(TAG, msg);
    }

    @ReactMethod
    public void e(String msg) {
        Log.e(TAG, msg);
    }

    @ReactMethod
    public void f(String msg) {
        Log.f(TAG, msg);
    }

    @ReactMethod
    public void flush() {
        Log.appenderFlush();
    }

    @ReactMethod
    public void getLogPath(Promise promise) {
        final String SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
        final String logPath = SDCARD + "/tengits_mqtt/log";
        promise.resolve(logPath);
    }

    @ReactMethod
    public void getCachePath(Promise promise) {
        final String cachePath = getReactApplicationContext().getFilesDir() + "/xlog";
        promise.resolve(cachePath);
    }

    @ReactMethod
    public void getLogLevel(Promise promise) {
        promise.resolve(Log.getLogLevel());
    }

    @ReactMethod
    public void setLogLevel(int level) {
        Log.setLevel(level, false);
    }
}